package com.ruowei.modules.foodOrder.web.rest;

import com.ruowei.common.error.exception.CommonException;
import com.ruowei.common.querydsl.OptionalBooleanBuilder;
import com.ruowei.modules.foodOrder.domain.MerchantChangeLog;
import com.ruowei.modules.foodOrder.domain.QMerchantChangeLog;
import com.ruowei.modules.foodOrder.repository.MerchantChangeLogRepository;
import com.ruowei.modules.foodOrder.web.dto.MerchantChangeDTO;
import com.ruowei.modules.foodOrder.web.dto.MerchantChangeDetailDTO;
import com.ruowei.modules.foodOrder.web.dto.MerchantChangeLogQM;
import io.github.jhipster.web.util.PaginationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api")
@Transactional
@Api(tags = "商家修改日志")
public class MerchantChangeLogResource {

    private final MerchantChangeLogRepository merchantChangeLogRepository;
    private final QMerchantChangeLog QMC = QMerchantChangeLog.merchantChangeLog;

    public MerchantChangeLogResource(MerchantChangeLogRepository merchantChangeLogRepository) {
        this.merchantChangeLogRepository = merchantChangeLogRepository;
    }


    @GetMapping("/merchant-change-log/list")
    @ApiOperation(value = "商家修改日志列表")
    public ResponseEntity<List<MerchantChangeLog>> getMerchantChangeList(MerchantChangeLogQM qm, Pageable pageable) {
        OptionalBooleanBuilder predicate = new OptionalBooleanBuilder()
            .notEmptyAnd(QMC.updateTime::goe, qm.getUpdateTimeStart())
            .notEmptyAnd(QMC.updateTime::loe, qm.getUpdateTimeEnd());

        Page<MerchantChangeLog> page = merchantChangeLogRepository.findAll(predicate.build(), pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    @GetMapping("/merchant-change-log/{id}")
    @ApiOperation(value = "商家修改日志详情")
    public ResponseEntity<MerchantChangeDTO> getMerchantChangeDetail(@PathVariable Long id) {
        if (id == null) {
            throw new CommonException("ID不能为空");
        }
        Optional<MerchantChangeLog> merchantChangeLog = merchantChangeLogRepository.findById(id);
        if (!merchantChangeLog.isPresent()) {
            throw new CommonException("商家修改日志详情不存在");
        }
        MerchantChangeDTO dto = new MerchantChangeDTO();
        merchantChangeLog.ifPresent(log -> {
            MerchantChangeDetailDTO changeInfo = new MerchantChangeDetailDTO();
            changeInfo.setId(log.getId());
            changeInfo.setMerchantCode(log.getMerchantCode());
            changeInfo.setMerchantName(log.getMerchantName());
            changeInfo.setMerchantAddress(log.getMerchantAddress());
            changeInfo.setPhone(log.getPhone());
            changeInfo.setDescription(log.getDescription());
            changeInfo.setNotice(log.getNotice());
            changeInfo.setOpenTime(log.getOpenTime());
            changeInfo.setStatus(log.getStatus());
            changeInfo.setStartingPrice(log.getStartingPrice());
            changeInfo.setLoginCode(log.getLoginCode());
            changeInfo.setPassword(log.getPassword());
            dto.setChangeInfo(changeInfo);
            //修改前日志
            MerchantChangeDetailDTO changeBefore = new MerchantChangeDetailDTO();
            Optional<MerchantChangeLog> changeLog = merchantChangeLogRepository.getMerchantChange(log.getMerchantCode(), log.getUpdateTime());
            if (changeLog.isPresent()) {
                MerchantChangeLog detail = changeLog.get();
                changeBefore.setId(log.getId());
                changeBefore.setMerchantCode(detail.getMerchantCode());
                changeBefore.setMerchantName(detail.getMerchantName());
                changeBefore.setMerchantAddress(detail.getMerchantAddress());
                changeBefore.setPhone(detail.getPhone());
                changeBefore.setDescription(detail.getDescription());
                changeBefore.setNotice(detail.getNotice());
                changeBefore.setOpenTime(detail.getOpenTime());
                changeBefore.setStatus(detail.getStatus());
                changeBefore.setStartingPrice(detail.getStartingPrice());
                changeBefore.setLoginCode(detail.getLoginCode());
                changeBefore.setPassword(detail.getPassword());
                dto.setChangeBefore(changeBefore);
            } else {
                dto.setChangeBefore(changeInfo);
            }
        });
        return ResponseEntity.ok(dto);
    }
}
